﻿using System.Buffers.Text;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Coravel.Queuing.Interfaces;
using CRM.Jobs;
using CRM.Services.Email;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;

namespace CRM.Controllers
{
    [AllowAnonymous]
    [Route("/[controller]/[action]")]
    public class EmailController : BaseController
    {
        private readonly EmailMarketingService _emailMarketingService;
        private readonly IWebHostEnvironment _environment;
        private readonly IQueue _queue;

        public EmailController(EmailMarketingService emailMarketingService, IWebHostEnvironment environment, IQueue queue)
        {
            _emailMarketingService = emailMarketingService;
            _environment = environment;
            _queue = queue;
        }

        /// <summary>
        /// 邮件阅读计数
        /// </summary>
        /// <param name="i">email marketing id</param>
        /// <param name="r">base64 邮箱</param>
        /// <returns></returns>
        public IActionResult Read(string i, string r)
        {
            _queue.QueueInvocableWithPayload<EmailReadEvent, EmailReadEventParam>(new EmailReadEventParam(i, r));
            var path = Path.Combine(_environment.WebRootPath, "img/1pixel.png");
            return new PhysicalFileResult(path, "image/png");
        }
    }
}